package com.ruoyi.web.controller.fund;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.archiveCabinet.domain.ArchiveCabinet;
import com.ruoyi.archiveCabinet.service.IArchiveCabinetService;
import com.ruoyi.filingCabinet.domain.FilingCabinet;
import com.ruoyi.filingCabinet.service.IFilingCabinetService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 档案管理Controller
 * 
 * @author lijie
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/fund/archiveCabinet")
public class ArchiveCabinetController extends BaseController
{
    @Autowired
    private IArchiveCabinetService archiveCabinetService;

    @Autowired
    private IFilingCabinetService filingCabinetService;

    /**
     * 查询档案管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ArchiveCabinet archiveCabinet)
    {
        List<ArchiveCabinet> list = archiveCabinetService.selectArchiveCabinetList(archiveCabinet);
        for(ArchiveCabinet archiveCabinet1:list){
            if(archiveCabinet1.getArchiveDescription()==null){
                archiveCabinet1.setArchiveDescription("");
            }
            if(archiveCabinet1.getArchiveDescription1()==null){
                archiveCabinet1.setArchiveDescription1("");
            }
            if(archiveCabinet1.getArchiveDescription().length()>10){
                archiveCabinet1.setArchiveDescription(archiveCabinet1.getArchiveDescription().substring(0,10)+"...");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出档案管理列表
     */
    @Log(title = "档案管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchiveCabinet archiveCabinet)
    {
        List<ArchiveCabinet> list = archiveCabinetService.selectArchiveCabinetList(archiveCabinet);
        ExcelUtil<ArchiveCabinet> util = new ExcelUtil<ArchiveCabinet>(ArchiveCabinet.class);
        util.exportExcel(response, list, "档案管理数据");
    }

    /**
     * 获取档案管理详细信息
     */
    @GetMapping(value = "/{archiveId}")
    public AjaxResult getInfo(@PathVariable("archiveId") Long archiveId)
    {
        return AjaxResult.success(archiveCabinetService.selectArchiveCabinetById(archiveId));
    }

    /**
     * 新增档案管理
     */
    @Log(title = "档案管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveCabinet archiveCabinet)
    {
        archiveCabinet.setCreater(getUsername());
        return toAjax(archiveCabinetService.insertArchiveCabinet(archiveCabinet));
    }

    /**
     * 修改档案管理
     */
    @Log(title = "档案管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveCabinet archiveCabinet)
    {
        return toAjax(archiveCabinetService.updateArchiveCabinet(archiveCabinet));
    }

    /**
     * 删除档案管理
     */
    @Log(title = "档案管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{archiveId}")
    public AjaxResult remove(@PathVariable Long archiveId) throws Exception {
        FilingCabinet filingCabinet = new FilingCabinet();
        filingCabinet.setArchiveId(archiveId);
        List<FilingCabinet> list = filingCabinetService.selectFilingCabinetList(filingCabinet);
        if(list.size()>0){
            throw new Exception("当前档案室中存在档案柜不允许删除！");
        }else{
            archiveCabinetService.deleteArchiveCabinetById(archiveId);
        }
        return toAjax(1);
    }
}
